package databases

import (
	"log"
	"time"
	"video/api/lib/utils"
	"video/api/models"
)

func AddUserCredential(loginName string, pwd string) error {
	stmtns, err := dbConn.Prepare("INSERT INTO users (login_name,pwd) VALUES (?,?)")
	log.Printf("err:%s",err)
	if err != nil {
		return err
	}
	stmtns.Exec(loginName, pwd)
	stmtns.Close()
	return nil
}

func GetUserCredential(loginName string) (string, error) {
	stmtOut, err := dbConn.Prepare("SELECT pwd from users where login_name=?")
	if err != nil {
		log.Printf("%s", err)
		return "", err
	}
	var pwd string

	err = stmtOut.QueryRow(loginName).Scan(&pwd)
	stmtOut.Close()
	if err != nil {
		log.Printf("%s", err)
		return "", err
	}

	return pwd, nil
}

func DeleteUser(loginName string, pwd string) error {
	stmtDel, err := dbConn.Prepare("DELETE FROM users WHERE login_name=? and pwd=?")
	if err != nil {
		log.Printf("%s", err)
		return err
	}
	stmtDel.Exec(loginName, pwd)
	stmtDel.Close()
	return nil
}

func AddNewVideo(aid int, name string) (*models.Video, error) {
	vid, err := utils.NewUUID()
	if err != nil {
		return nil, err
	}

	t := time.Now()
	ctime := t.Format("2006-01-02 15:04:05")
	stmtIns, err := dbConn.Prepare("INSERT INTO video_info (id,author_id,name,display_ctime,create_time) value(?,?,?,?,?)")
	if err != nil {
		return nil, err
	}
	defer stmtIns.Close()
	_, err = stmtIns.Exec(vid, aid, name, ctime, ctime)
	if err != nil {
		return &models.Video{}, nil
	}
	return &models.Video{Id: vid, AuthorId: aid, Name: name, DisplayCtime: ctime}, nil
}

func GetVideoInfo(vid string) (*models.Video, error) {
	stmtOut, err := dbConn.Prepare("SELECT id,author_id,name,display_ctime FROM video_info where id=?")
	video := models.Video{}
	if err != nil {
		return &video, err
	}
	defer stmtOut.Close()

	var id, name, display_ctime string
	var author_id int

	err = stmtOut.QueryRow(vid).Scan(&id, &author_id, &name, &display_ctime)
	if err != nil {
		return &video, err
	}
	video.Id = id
	video.Name = name
	video.AuthorId = author_id
	video.DisplayCtime = display_ctime

	return &video, nil
}

func DeleteVideoInfo(vid string) error {
	stmtDel, err := dbConn.Prepare("DELETE FROM video_info where id=?")
	if err != nil {
		return err
	}
	defer stmtDel.Close()
	result, err := stmtDel.Exec(vid)
	if err != nil {
		return err
	}
	_, err = result.RowsAffected()
	if err != nil {
		return err
	}
	return nil
}

func AddNewComments(vid string, aid int, content string) error {
	stmtIn, err := dbConn.Prepare("INSERT INTO comments (id,video_id,author_id,content,time) value(?,?,?,?,?)")
	if err != nil {
		return err
	}
	defer stmtIn.Close()
	t := time.Now()
	ctime := t.Format("2006-01-02 15:04:05")

	uuid, err := utils.NewUUID()
	if err != nil {
		return  err
	}

	result, err := stmtIn.Exec(uuid, vid, aid, content, ctime)
	if err != nil {
		return err
	}
	if _, err = result.LastInsertId(); err != nil {
		return err
	}
	return nil

}

func ListComments(vid string, from, to int) ([]*models.Comment, error) {

	stmtOut, err := dbConn.Prepare(` SELECT comments.id, users.Login_name, comments.content FROM comments
		INNER JOIN users ON comments.author_id = users.id
		WHERE comments.video_id = ? AND comments.time > FROM_UNIXTIME(?) AND comments.time <= FROM_UNIXTIME(?)`)
	var comments []*models.Comment
	if err != nil {
		return comments, err
	}
	defer stmtOut.Close()

	rows, err := stmtOut.Query(vid, from, to)
	if err != nil {
		return comments, err
	}

	for rows.Next() {
		var id, name, content string
		if err = rows.Scan(&id, &name, &content); err != nil {
			continue
		}
		comments = append(comments, &models.Comment{
			Id:      id,
			VideoId: vid,
			Author:  name,
			Content: content})
	}
	return comments,nil

}
